function nearest_data(node) {
  return nearest_datacontainer(node)?.thin_data;
}

function nearest_datacontainer(node) {
  while (node && !node.thin_data) {
    node = node.parentNode;
  }
  return node;
}

function nearest_osspath(node) {
  while (node && !node?.thin_data?.osspath) {
    node = node.parentNode;
  }
  return node?.thin_data?.osspath;
}

function nearest_formview(node) {
  return node.closest("formview");
}

function nearest_param(node) {
  while (node && node.nodeName.toUpperCase() !== "ROUTE") {
    node = node.parentNode;
  }
  // console.log({ node });
  if (node) return node.thin_param;
  else return;
}

function nearest_render(node) {
  while (node && !node.thin_render) {
    node = node.parentNode;
  }
  return node;
}

function nearest_querycontainer(context) {
  let query_container = context;
  while (
    query_container !== undefined &&
    query_container?.thin_query === undefined
  ) {
    query_container = query_container.parentNode;
  }
  return query_container;
}

export {
  nearest_data,
  nearest_datacontainer,
  nearest_osspath,
  nearest_param,
  nearest_render,
  nearest_formview,
  nearest_querycontainer,
};
